package com.cyan.note.controller.manager;

import com.cyan.note.common.ApiResult;
import com.cyan.note.common.PageResult;
import com.cyan.note.dto.manager.order.*;
import com.cyan.note.service.manager.ManagerOrderService;
import com.cyan.note.util.SecurityUtils;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 店长端-订单管理控制器
 * 
 * @author ZhangzhenYu
 * @since 2025-01-15
 */
@RestController
@RequestMapping("/api/manager/orders")
public class ManagerOrderController {

    @Autowired
    private ManagerOrderService managerOrderService;

    /**
     * 分页查询订单列表
     */
    @GetMapping("/list")
    public ApiResult<PageResult<OrderListDTO>> getOrderList(
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String orderType,
            @RequestParam(required = false) String orderSource,
            @RequestParam(required = false) String customerName,
            @RequestParam(required = false) String workerName,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate
    ) {
        Long shopId = SecurityUtils.getCurrentShopId();
        OrderQueryDTO query = new OrderQueryDTO();
        query.setCurrent(current);
        query.setSize(size);
        query.setStatus(status);
        query.setOrderType(orderType);
        query.setOrderSource(orderSource);
        query.setCustomerName(customerName);
        query.setWorkerName(workerName);
        query.setStartDate(startDate);
        query.setEndDate(endDate);
        
        return managerOrderService.getOrderList(shopId, query);
    }

    /**
     * 获取订单详情
     */
    @GetMapping("/detail/{orderId}")
    public ApiResult<OrderDetailDTO> getOrderDetail(@PathVariable Long orderId) {
        Long shopId = SecurityUtils.getCurrentShopId();
        return managerOrderService.getOrderDetail(shopId, orderId);
    }

    /**
     * 手动创建订单（客服下单）
     */
    @PostMapping("/create")
    public ApiResult<Long> createOrder(@Valid @RequestBody OrderCreateDTO orderCreateDTO) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        return managerOrderService.createOrder(shopId, orderCreateDTO, operatorId);
    }

    /**
     * 分配订单给打手
     */
    @PostMapping("/assign/{orderId}")
    public ApiResult<Void> assignOrder(
            @PathVariable Long orderId,
            @Valid @RequestBody OrderAssignDTO orderAssignDTO
    ) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        return managerOrderService.assignOrder(shopId, orderId, orderAssignDTO, operatorId);
    }

    /**
     * 手动结单
     */
    @PostMapping("/complete/{orderId}")
    public ApiResult<Void> completeOrder(
            @PathVariable Long orderId,
            @RequestBody(required = false) OrderCompleteDTO orderCompleteDTO
    ) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        return managerOrderService.completeOrder(shopId, orderId, orderCompleteDTO, operatorId);
    }

    /**
     * 确认收款
     */
    @PostMapping("/confirm-payment/{orderId}")
    public ApiResult<Void> confirmPayment(
            @PathVariable Long orderId,
            @Valid @RequestBody OrderPaymentDTO orderPaymentDTO
    ) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        return managerOrderService.confirmPayment(shopId, orderId, orderPaymentDTO, operatorId);
    }

    /**
     * 炸单处理
     */
    @PostMapping("/cancel/{orderId}")
    public ApiResult<Void> cancelOrder(
            @PathVariable Long orderId,
            @Valid @RequestBody OrderCancelDTO orderCancelDTO
    ) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        return managerOrderService.cancelOrder(shopId, orderId, orderCancelDTO, operatorId);
    }

    /**
     * 重新分配订单（炸单后）
     */
    @PostMapping("/reassign/{orderId}")
    public ApiResult<Void> reassignOrder(
            @PathVariable Long orderId,
            @Valid @RequestBody OrderAssignDTO orderAssignDTO
    ) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        return managerOrderService.reassignOrder(shopId, orderId, orderAssignDTO, operatorId);
    }

    /**
     * 获取可分配的打手列表
     */
    @GetMapping("/available-workers")
    public ApiResult<List<AvailableWorkerForOrderDTO>> getAvailableWorkers(
            @RequestParam(required = false) String orderType,
            @RequestParam(required = false) String startTime
    ) {
        Long shopId = SecurityUtils.getCurrentShopId();
        return managerOrderService.getAvailableWorkers(shopId, orderType, startTime);
    }

    /**
     * 获取订单状态统计
     */
    @GetMapping("/status-stats")
    public ApiResult<OrderStatusStatsDTO> getOrderStatusStats(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate
    ) {
        Long shopId = SecurityUtils.getCurrentShopId();
        return managerOrderService.getOrderStatusStats(shopId, startDate, endDate);
    }

    /**
     * 批量操作订单
     */
    @PostMapping("/batch")
    public ApiResult<Void> batchOperateOrders(@Valid @RequestBody OrderBatchOperateDTO batchOperateDTO) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        return managerOrderService.batchOperateOrders(shopId, batchOperateDTO, operatorId);
    }

    /**
     * 导出订单数据
     */
    @GetMapping("/export")
    public ApiResult<String> exportOrders(
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(defaultValue = "excel") String format
    ) {
        Long shopId = SecurityUtils.getCurrentShopId();
        return managerOrderService.exportOrders(shopId, status, startDate, endDate, format);
    }
}